/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProviderStreamWorker.Utils;

/**
 *
 * @author fabualves
 */
public class ByteArray {

    public static int toInt(byte[] b, int offset, int size) {
        int value = 0;
        for (int i = 0; i < size; i++) {
            int shift = (size - 1 - i) * 8;
            value += (b[i + offset] & 0x000000FF) << shift;
        }
        return value;
    }

    public static byte[] zeroFillAllocate(byte[] byteArray, int expected) {
        int length = byteArray.length;
        if (length < expected) {
            byte[] newArray = new byte[expected];
            int difference = expected - length;
            for (int i = expected - 1; i >= 0; i--) {
                if (i >= difference) {
                    newArray[i] = byteArray[i - difference];
                } else {
                    newArray[i] = 0;
                }
            }
            return newArray;
        } else {
            return byteArray;
        }
    }
}
